package com.weslide.weslideapp.fragment;

import org.json.JSONException;

import com.gt.cl.http.CLConnectionException;
import com.gt.cl.http.CLInvalidNetworkException;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.weslide.weslideapp.R;
import com.weslide.weslideapp.activity.FillExchangeFormActivity;
import com.weslide.weslideapp.activity.GiftDetailActivity;
import com.weslide.weslideapp.manager.api.APIManager;
import com.weslide.weslideapp.manager.api.LoginManager;
import com.weslide.weslideapp.pojo.ao.GiftAO;
import com.weslide.weslideapp.util.TMTPAsyncTask;

import android.R.raw;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class GoodsDetailFragment extends WeSlideFragment {
	public static final String TAG = "GoodsDetailFragment";
	private String goodId;
	private ImageView mImgBack;
	private TextView mTvScorePrice;
	private TextView mTvPrice;
	private TextView mTvGoodsName;
	private TextView mTvDescription1;
	private TextView mTvDescription2;
	private TextView mTvDescription3;
	private TextView mTvMyScore;
	private View mTvExchange;
	private TextView mTvmerchantPhone;
	private TMTPAsyncTask<Void, Void, Void> asyncTask;
	private GiftAO giftAO;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		globalView = inflater.inflate(R.layout.page_goods_detail, container,
				false);
		findViews();
		init();
		return globalView;
	}

	private void findViews() {
		mImgBack = (ImageView) findViewById(R.id.page_goods_detail_mImgVBack);
		mTvScorePrice = (TextView) findViewById(R.id.page_goods_detail_mTvScorePrice);
		mTvPrice = (TextView) findViewById(R.id.page_goods_detail_mTvPrice);
		mTvGoodsName = (TextView) findViewById(R.id.page_goods_detail_mTvGoodsName);
		mTvDescription1 = (TextView) findViewById(R.id.page_goods_detail_mTvDescription1);
		mTvDescription2 = (TextView) findViewById(R.id.page_goods_detail_mTvDescription2);
		mTvDescription3 = (TextView) findViewById(R.id.page_goods_detail_mTvDescription3);
		mTvMyScore = (TextView) findViewById(R.id.page_goods_detail_mTvMyScore);
		mTvExchange = findViewById(R.id.page_goods_detail_mTvExchange);
		mTvmerchantPhone = (TextView) findViewById(R.id.page_goods_detail_mTvMerchantPhone);
		mTvExchange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int needScore = Integer.parseInt(giftAO.getNeedScore());
				int myScore = Integer.parseInt(LoginManager.getInstance()
						.getTotalScore());
				Log.i(TAG, "myScore >>> " + myScore);
				if (needScore > myScore) {
					Toast.makeText(getActivity(), "积分余额不足", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				Intent intent = new Intent();
				intent.setClass(getActivity(), FillExchangeFormActivity.class);
				Bundle bundel = new Bundle();
				bundel.putString(GiftDetailActivity.EXTRA_GOODID, goodId);
				bundel.putString("goodName", giftAO.getName());
				bundel.putString("needScore", giftAO.getNeedScore());
				bundel.putString("myscore", "" + myScore);
				bundel.putString("price", giftAO.getMoney());
				intent.putExtras(bundel);
				startActivity(intent);

			}
		});
		mImgBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getActivity().onBackPressed();
			}
		});
		mTvmerchantPhone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				makeCall();
			}

		});
		findViewById(R.id.page_goods_detail_mLlPhone).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						makeCall();
					}
				});
	}

	public void makeCall() {
		String mobile = giftAO.getPhone();
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
				+ mobile));
		startActivity(intent);
	}

	private void init() {
		Bundle bundle = getArguments();
		goodId = bundle.getString(GiftDetailActivity.EXTRA_GOODID);
		asyncTask = new TMTPAsyncTask<Void, Void, Void>(getActivity()) {

			@Override
			protected void doOnSuccess(Void result) {
				mTvGoodsName.setText(giftAO.getName());
				mTvPrice.setText("价值" + giftAO.getMoney() + "元");
				Log.i(TAG, "money >>> " + giftAO.getMoney());
				mTvScorePrice.setText(giftAO.getNeedScore() + "积分");
				mTvMyScore.setText("我的积分："
						+ LoginManager.getInstance().getTotalScore());
				mTvmerchantPhone.setText(giftAO.getPhone());
				mTvDescription1.setText(giftAO.getOcount()
						+ "人兑换,剩余"
						+ (Integer.parseInt(giftAO.getTcount()) - Integer
								.parseInt(giftAO.getOcount())) + "份");
				mTvDescription2.setText(giftAO.getDescription());
				mTvDescription3.setText(giftAO.getRemark());
			}

			@Override
			protected Void doInBackground(Void params)
					throws CLInvalidNetworkException, CLConnectionException {

				try {
					giftAO = APIManager.getInstance().getGiftDetail(goodId);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				return null;
			}
		};
		asyncTask.execute();
	}

}
